﻿Public Class TaskManagerImpl
    Inherits ManagerImpl(Of Task)
    Implements TaskManager

    Private Class TaskMapper
        Implements RowMapper(Of Task)
        Public Function map(ByRef reader As System.Data.OleDb.OleDbDataReader) As Task Implements RowMapper(Of Task).map
            Dim task As New Task()
            task.id = reader("id")
            task.titel = reader("titel")
            Return task
        End Function
    End Class

    Sub New()
        MyBase.New("task", New TaskMapper())
    End Sub

    Public Overrides Sub insert(ByRef entity As Task)
        Dim query As New Query()
        query.sql = "insert into " & tableName & " (projekt_id, titel) values (:projektId, :titel)"
        query.setParameter("projektId", entity.projekt.id)
        query.setParameter("titel", entity.titel)
        runQuery(query)
    End Sub

    Public Overrides Sub update(ByRef entity As Task)
        Dim query As New Query()
        query.sql = "update " & tableName & " set projekt_id=:projektId, titel=:titel where id=:id"
        query.setParameter("projektId", entity.projekt.id)
        query.setParameter("titel", entity.titel)
        query.setParameter("id", entity.id)
        runQuery(query)
    End Sub

    Public Function findByProjekt(ByRef projekt As Projekt) As System.Collections.Generic.List(Of Task) Implements TaskManager.findByProjekt
        Dim query As New Query()
        query.sql = "select * from " & tableName & " where projekt_id=:projektId"
        query.setParameter("projektId", projekt.id)
        Return selectQuery(query, New TaskMapper())
    End Function

    Public Sub deleteByProjekt(ByRef projekt As Projekt) Implements TaskManager.deleteByProjekt
        Dim query As New Query()
        query.sql = "delete from " & tableName & " where projekt_id=:projektId"
        query.setParameter("projektId", projekt.id)
        runQuery(query)
    End Sub
End Class
